package com.uzai.console.web.common;


import com.baomidou.mybatisplus.extension.api.ApiController;
import com.uzai.common.enums.WorkOrderType;
import com.uzai.common.exception.BusinessException;
import com.uzai.console.dto.WorkOrderQueryDTO;
import com.uzai.console.dto.workorder.WorkOrderOptDTO;
import com.uzai.console.dto.workorder.WorkOrderReplyDTO;
import com.uzai.console.service.WorkOrderService;
import com.uzai.console.service.main.MainService;
import com.uzai.console.vo.workorder.WorkOrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 工单(WorkOrder)表控制层
 *
 * @author mingyang.chen
 * @since 2023 -06-16 14:42:58
 */
@RestController
@RequestMapping("workOrder")
@Api(value = "工单控制器", tags = "工单控制器")
public class WorkOrderController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private WorkOrderService workOrderService;

    @Autowired
    private MainService mainService;

    /**
     * 分页查询所有数据
     *
     * @param param the param
     */
    @PostMapping("/search")
    @ApiOperation(value = "工单搜索", tags = "工单搜索", response = WorkOrderVO.class)
    public Object selectAll(@RequestBody WorkOrderQueryDTO param) {
        return this.workOrderService.page(param);
    }

    /**
     * 新增数据
     *
     * @param opt the opt
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增工单", tags = "新增工单", response = Boolean.class)
    public Object insert(@RequestBody WorkOrderOptDTO opt) {
        if (Objects.isNull(opt) || ObjectUtils.anyNull(opt.getType())
                || StringUtils.isAnyBlank(opt.getDesc())) {
            throw new BusinessException("请求参数错误");
        }
        WorkOrderType byCode = WorkOrderType.getByCode(opt.getType());
        if (Objects.isNull(byCode)) {
            throw new BusinessException("工单类型错误");
        }
        if (!mainService.checkJoinGroup(opt.getMerId())) {
            throw new BusinessException("您未加入专属群, 无法提交工单");
        }
        return this.workOrderService.add(opt);
    }


    /**
     * 回复
     *
     * @param opt the opt
     */
    @PostMapping("/reply")
    @ApiOperation(value = "回复工单", tags = "回复工单", response = Boolean.class)
    public Object reply(@RequestBody WorkOrderReplyDTO opt) {
        if (Objects.isNull(opt) || ObjectUtils.anyNull(opt.getId())
                || StringUtils.isAnyBlank(opt.getReply())) {
            throw new BusinessException("请求参数错误");
        }
        return this.workOrderService.reply(opt);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除工单", tags = "删除工单", response = Boolean.class)
    public Object delete(@RequestBody WorkOrderOptDTO opt) {
        if (Objects.isNull(opt) || ObjectUtils.anyNull(opt.getId())) {
            throw new BusinessException("请求参数错误");
        }
        return this.workOrderService.delete(opt.getId());
    }
}

